package com.mdd.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.order.IRechargeOrderService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.order.RechargeOrderParam;
import com.mdd.admin.vo.order.OrderListVo;
import com.mdd.admin.vo.order.RechargeOrderListVo;
import com.mdd.admin.vo.order.RechargeOrderDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.Order;
import com.mdd.common.entity.order.RechargeOrder;
import com.mdd.common.mapper.order.RechargeOrderMapper;
import com.mdd.common.utils.ArrayUtil;
import com.mdd.common.utils.StringUtil;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import com.mdd.common.config.GlobalConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 充值订单实现类
 * @author panweiliang
 */
@Service
public class RechargeOrderServiceImpl implements IRechargeOrderService {
        
    @Resource
    RechargeOrderMapper rechargeOrderMapper;

    /**
     * 充值订单列表
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return PageResult<RechargeOrderListVo>
     */
    @Override
    public PageResult<RechargeOrderListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page  = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();

        MPJQueryWrapper<RechargeOrder> mpjQueryWrapper = new MPJQueryWrapper<RechargeOrder>()
                .selectAll(RechargeOrder.class)
                .select("user.nickname as nickname, user.id as userId, user.avatar AS avatar ")
                .innerJoin("?_user user ON user.id=t.user_id".replace("?_", GlobalConfig.knowledgePaymentTablePrefixe))
                .orderByDesc(Arrays.asList("t.id"));

        if (StringUtil.isNotEmpty(params.get("keyword"))) {
            String keyword = params.get("keyword");
            mpjQueryWrapper.nested(wq->wq
                    .like("user.sn", keyword).or()
                    .like("user.nickname", keyword).or()
                    .like("user.mobile", keyword));
        }

        rechargeOrderMapper.setSearch(mpjQueryWrapper, params, new String[]{
                "like:sn@t.sn:str",
                "datetime:startTime-endTime@t.create_time:str",
                "=:payStatus@pay_status:int",
                "=:payWay@pay_way:int",
        });
        IPage<RechargeOrderListVo> iPage = rechargeOrderMapper.selectJoinPage(new Page<>(page, limit), RechargeOrderListVo.class, mpjQueryWrapper);
        for(RechargeOrderListVo item : iPage.getRecords()) {
            item.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
            item.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
            item.setPayTime(TimeUtil.timestampToDate(item.getPayTime()));
            item.setAvatar(UrlUtil.toAbsoluteUrl(item.getAvatar()));
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    /**
     * 获取有效充值订单总金额
     *
     * @author panweiliang
     * @return BigDecimal
     */
    public BigDecimal getRechargeAmount() {
        return this.rechargeOrderMapper.getRechargeAmount();
    }

    @Override
    public Long getRechargeOrderCountByPayStatus(Integer payStatus) {
        return this.rechargeOrderMapper.getRechargeOrderCountByPayStatus(payStatus);
    }
}
